package me.stone.stanimeclient.core.ui.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import me.stone.stanimeclient.core.model.FanDailyUpdateItem
import me.stone.stanimeclient.core.ui.adapters.base.AbsViewDataBoundAdapter
import me.stone.stanimeclient.core.ui.databinding.ItemUpdateLineBinding
import java.util.concurrent.Executor


class FanDailyUpdateAdapter (
    backExecutor: Executor
): AbsViewDataBoundAdapter<FanDailyUpdateItem, ItemUpdateLineBinding>(
    backExecutor = backExecutor,
    diffCallback = object : DiffUtil.ItemCallback<FanDailyUpdateItem>() {
        override fun areItemsTheSame(
            oldItem: FanDailyUpdateItem,
            newItem: FanDailyUpdateItem
        ): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: FanDailyUpdateItem,
            newItem: FanDailyUpdateItem
        ): Boolean {
            return oldItem == newItem
        }
    }
) {

    override fun createDataBinding(parent: ViewGroup): ItemUpdateLineBinding {
        return ItemUpdateLineBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
    }

    override fun bindData(
        binding: ItemUpdateLineBinding,
        item: FanDailyUpdateItem,
        position: Int
    ) {
        binding.item = item
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recyclerView.itemAnimator = null
        recyclerView.setHasFixedSize(true)
        if (recyclerView.itemDecorationCount == 0) {
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    DividerItemDecoration.VERTICAL
                )
            )
        }
    }

}